<?php

namespace app\merchant\model\user;

use app\common\model\user\DealerLog as DealerLogModel;

/**
 * 用户佣金变动明细模型
 * Class BalanceLog
 * @package app\store\model\user
 */
class DealerLog extends DealerLogModel
{
    /**
     * 获取佣金变动明细列表
     * @param array $query
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($query = [])
    {
        //搜索条件
        $param = array_merge([
            'user_id' => 0,
            'search' => '',
            'scene' => -1,
            'start_time' => '',
            'end_time' => '',
        ], $query);

        // 设置查询条件
        $this->setWhere($param);
        // 获取列表数据
        return $this->alias('log')
            ->join('user', 'user.user_id = log.user_id')
            ->field('log.id,log.user_id,log.flow_type,log.money,log.describe,log.create_time,user.user_id,user.nickName,user.avatarUrl')
            ->order(['log.create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);


    }

    /**
     * 设置查询条件
     * @param $query
     */
    private function setWhere($query)
    {
        $this->where('log.money', '<>', 0);
        // 用户ID
        $query['user_id'] > 0 && $this->where('log.user_id', '=', (int)$query['user_id']);
        // 用户昵称
        !empty($query['search']) && $this->where('user.nickName', 'like', "%{$query['search']}%");
        // 余额变动场景
        $query['scene'] > -1 && $this->where('log.flow_type', '=', (int)$query['scene']);
        // 起始时间
        !empty($query['start_time']) && $this->where('log.create_time', '>=', strtotime($query['start_time']));
        // 截止时间
        !empty($query['end_time']) && $this->where('log.create_time', '<', strtotime($query['end_time']) + 86400);
    }

}